home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / makeRegularPolygon.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  2.9 KB  |  114 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. // Description : make a regular polygon given the number of sides
  19. // and the length of a side. 
  20. //
  21. // Usage :
  22. // 1. For a equilateral triangle of length 3 : makeRegularPolyon(3,2);
  23. // 2. For a hexagon of length 2.5 : makeRegularPolyon(6,2.5);
  24. //
  25. // Note : number of sides should be atleast 3. We could build
  26. // regular triangles, squares, pentagon, hexagon, ...., circle in the 
  27. // limit. ? The polygon is created in the XY ( Z=0) plane.
  28.  
  29. //
  30. // Find the radius of circle encompasing the polygon. 
  31. //
  32. proc float boundingCircleRadius( int $n, float $x )
  33. //
  34. //    Description:
  35. //        number of sides on the polygon.
  36. //        $x = length of the polygon.
  37. //
  38. {
  39.     float $rad ;
  40.     float $angle = 360.0 / $n ;
  41.  
  42.     float $cosx = cos(deg_to_rad($angle)) ;     
  43.     float $den = 2.0 * ( 1.0 - $cosx ) ;
  44.     $rad = $x / sqrt($den) ;
  45.     return $rad ;
  46. }
  47.  
  48. proc float[] createRegularPolygonY( int $n, float $r )
  49. {
  50.     int $i ;
  51.     float $x[] ;
  52.     float $angle = 360.0 / $n ;
  53.  
  54.     for( $i = 0 ; $i < $n ; $i++ ) {
  55.         float $a = $i * $angle ;    
  56.         $x[$i] =  $r * cos(deg_to_rad($a)) ;
  57.     }
  58.     return $x ;    
  59. }
  60.  
  61. proc float[] createRegularPolygonX( int $n,float $r )
  62. {
  63.     int $i ;
  64.     float $x[] ;
  65.     float $angle = 360.0 / $n ;
  66.     for( $i = 0 ; $i < $n ; $i++ ) {
  67.         float $a = $i * $angle ;    
  68.         $x[$i] =  $r * sin(deg_to_rad($a)) ;
  69.     }
  70.     return $x ;    
  71. }
  72.  
  73.  
  74. global proc int makeRegularPolygon( int $n, float $len )
  75.  
  76. {
  77.  
  78.     if( $n <= 2 ) {
  79.         error "A regular polygon must have atleast 3 sides" ;
  80.         return 1 ;
  81.     }
  82.  
  83.     float $r = boundingCircleRadius( $n, $len ) ;
  84.  
  85.     float $x[] = createRegularPolygonX( $n, $r ) ;
  86.     float $y[] = createRegularPolygonY( $n, $r ) ;
  87.  
  88.     // create the n-sided polygon. 
  89.     //
  90.     string $facet[] ;
  91.     string $cmd = "polyCreateFacet -ch 0  ";
  92.     string $vertStr ;
  93.     float $z = 0.0 ;
  94.     int $i ;
  95.     for( $i = 0 ; $i < $n ; $i++ ) {
  96.         $vertStr += " -p " ;
  97.         $vertStr += $x[$i] ;
  98.         $vertStr += " " ;
  99.         $vertStr += $y[$i] ;
  100.         $vertStr += " " ;
  101.         $vertStr += $z ;
  102.     }
  103.     $cmd += $vertStr ; 
  104.     $cmd += " "; 
  105.  
  106.     // do the polyCreateFacet cmd.
  107.     //
  108.     string $facet[] = eval($cmd) ;
  109.     select -r $facet[0] ;
  110.     return 0 ;
  111.  
  112. }
  113.  
  114.